﻿using Basic.FormCollect.Model;
using Basic.FormModel.QueryGroup;
using Basic.FormRemoteModel;
using Basic.FormRemoteModel.QueryForm.Model;
using WeDonekRpc.Helper;

namespace Basic.UserFormService.PhysicsTable
{
    internal static class LinqHelper
    {
        public static QueryTableBody GetQueryTable ( this PhysicsQueryTable table )
        {
            return new QueryTableBody
            {
                Id = table.Id,
                Column = table.GetColumn(false, false)
            };
        }
        public static QueryTableCol[] GetColumn ( this PhysicsQueryTable table, long[] columnId )
        {
            QueryTableCol[] cols = table.GetColumn(true, true);

            return cols.FindAll(c =>
            {
                if ( c.Children.IsNull() && !columnId.Contains(c.Id) )
                {
                    return false;
                }
                else if ( c.Children.IsNull() == false )
                {
                    c.Children = c.Children.FindAll(a => columnId.Contains(a.Id));
                    if ( c.Children.IsNull() )
                    {
                        return false;
                    }
                }
                return true;
            });
        }
        public static QueryTableCol[] GetColumn ( this PhysicsQueryTable table, bool isShowHide = false, bool isQueryHide = false )
        {
            List<QueryTableCol> list = new List<QueryTableCol>();
            table.Column.ForEach(( id, c ) =>
            {
                if ( c.GroupId == 0 )
                {
                    if ( isShowHide == false && c.IsHidden )
                    {
                        return;
                    }
                    else if ( isQueryHide == false && c.IsQueryHide )
                    {
                        return;
                    }
                    list.Add(new QueryTableCol
                    {
                        Id = c.Id,
                        Name = c.ColName,
                        Label = c.ColLabel,
                        ColSet = c.ColumnSet,
                        DataType = c.DataType,
                        FuncType = c.FuncType,
                        Align = c.Align,
                        IsSort = true,
                        SortName = c.ShowMode != PhyColShowMode.Column && c.SourceCol.HasValue ? table.Column[c.SourceCol.Value].ColName : null,
                        Width = c.Width,
                        Sort = c.Sort,
                    });
                }
            });
            table.Groups.ForEach(c =>
            {
                if ( c.ParentId == 0 )
                {
                    QueryTableCol t = _LoadColumn(c, table, isShowHide, isQueryHide);
                    if ( t != null )
                    {
                        list.Add(t);
                    }
                }
            });
            if ( list.Count == 0 )
            {
                return null;
            }
            return list.OrderBy(c => c.Sort).ToArray();
        }
        private static QueryTableCol _LoadColumn ( PhyQueryGroup group, PhysicsQueryTable table, bool isShowHidden, bool isQueryHidden )
        {
            List<QueryTableCol> list = new List<QueryTableCol>();
            table.Column.ForEach(( id, c ) =>
            {
                if ( c.GroupId == group.Id )
                {
                    if ( isShowHidden == false && c.IsHidden )
                    {
                        return;
                    }
                    else if ( isQueryHidden == false && c.IsQueryHide )
                    {
                        return;
                    }
                    list.Add(new QueryTableCol
                    {
                        Id = c.Id,
                        Name = c.ColName,
                        Label = c.ColLabel,
                        Align = c.Align,
                        DataType = c.DataType,
                        FuncType = c.FuncType,
                        ColSet = c.ColumnSet,
                        IsSort = true,
                        SortName = c.ShowMode != PhyColShowMode.Column && c.SourceCol.HasValue ? table.Column[c.SourceCol.Value].ColName : null,
                        Width = c.Width,
                        Sort = c.Sort,
                    });
                }
            });
            table.Groups.ForEach(c =>
            {
                if ( c.ParentId == group.Id )
                {
                    QueryTableCol t = _LoadColumn(c, table, isShowHidden, isQueryHidden);
                    if ( t != null )
                    {
                        list.Add(t);
                    }
                }
            });
            if ( list.Count == 0 )
            {
                return null;
            }
            return new QueryTableCol
            {
                Id = group.Id,
                Label = group.GroupName,
                Sort = group.Sort,
                Children = list.OrderBy(a => a.Sort).ToArray()
            };
        }

    }
}
